package com.wb.thread;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * TODO
 * 守护线程
 * @author wangTao
 * @version 1.0.1
 * @date 2020/7/9 下午 3:04
 */
public class DemoThread {

    private final Logger logger = LoggerFactory.getLogger(DemoThread.class);

    public static void main(String[] args) {
        System.out.println("main start=====");
        Thread thread1 = new Thread("守护线程"){
            @Override
            public void run() {
                int i = 0;
                while (i <= 4){
                    i++;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"："+i);
                }
                super.run();
            }
        };


        Thread thread2 = new Thread("用户线程"){
            @Override
            public void run() {
                int i = 0;
                while (i <= 2){
                    i++;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"："+i);
                }
                super.run();
            }
        };

        //setDaemon, 不设置则默认false
        thread1.setDaemon(false);//设置thread1为守护线程
        thread2.setDaemon(false);//设置thread2为普通线程

        thread1.start();
        thread2.start();

        System.out.println("main end==");

        Executors.newCachedThreadPool();
    }

    class ThreadFactoryClass implements ThreadFactory {

        private int index;

        public ThreadFactoryClass(int index) {
            this.index = index;
        }

        @Override
        public Thread newThread(Runnable r) {
            index++;
            return new Thread(r , "thread_" + index);
        }
    }

    class LinkedBlockingQueueClass<Runnable> extends LinkedBlockingQueue<Runnable> {

        public LinkedBlockingQueueClass(int size) {
            super(size);
        }

        @Override
        public boolean add(Runnable runnable) {
            logger.info("name : {} " , runnable.getClass().getName());
            return super.add(runnable);
        }

        @Override
        public void put(Runnable runnable) throws InterruptedException {
            logger.info("name : {} " , runnable.getClass().getName());
            super.put(runnable);
        }

        @Override
        public boolean offer(Runnable runnable) {
            logger.info("name : {} " , runnable.getClass().getName());
            return super.offer(runnable);
        }
    }

    @Test
    public void run1() {
        BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueueClass<Runnable>(50);
        String name = "thread_";
        ThreadFactory threadFactory = new ThreadFactoryClass(0);

        Executor executor = new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors() , // 正式工数量
                50, // 工人数量上限，包括正式工和临时工
        1, TimeUnit.SECONDS, // 临时工游手好闲的最长时间，超过这个时间将被解雇
                blockingQueue, // 排期队列
                threadFactory, // 招人渠道
                new ThreadPoolExecutor.AbortPolicy()); // 拒单方式

        int count = 0;
        for (int i = 0; i < 100; i++) {
            try {
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            logger.info("{}" , Thread.currentThread().getName());
                            Thread.sleep(10 * 1000L);
                        } catch (InterruptedException e) {
                            logger.error("" , e);
                        }
                    }
                });
            } catch (Exception e) {
                count++;
                logger.error("" , e);
            }
        }
        logger.info("count : {}" , count);
        try {
            Thread.sleep(999999999 * 1000L);
        } catch (InterruptedException e) {
            logger.error("" , e);
        }
    }

    public void queueTest() {
        LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue(2);
        linkedBlockingQueue.offer("1");
        linkedBlockingQueue.offer("2");

    }
}